<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date: 2010-04-24
 * -+-----------------------------------------
 *
 * 图形处理类
 * @author jinke
 */
class Image extends Base
{

	public function __construct()
	{
		Debug::initClass('image.class.php');
	}

	/**
	 * 生成图形验证码
	 * @access public
	 * @param int $Width 宽度[可选]
	 * @param int $Height 高度[可选]
	 * @return void
	 */
	public function verify($Width=50,$Height=20)
	{
		$mCheckCodeNum=4;
		header ("Content-type: image/png");
		$mCheckCode = strtoupper(substr(md5(rand()),0,$mCheckCodeNum));

		$mCheckImage = @imagecreate ($Width,$Height);
		Functions::REC('_session')->set('image_verify',md5(strtolower($mCheckCode)));
		imagecolorallocate ($mCheckImage, 200, 200, 200);
		for ($i=0;$i<=128;$i++)
		{
			$mDisturbColor = imagecolorallocate ($mCheckImage, rand(0,255), rand(0,255), rand(0,255));
			imagesetpixel($mCheckImage,rand(2,128),rand(2,38),$mDisturbColor);
		}
		for ($i=0;$i<$mCheckCodeNum;$i++)
		{
			$bg_color = imagecolorallocate ($mCheckImage, rand(0,255), rand(0,128), rand(0,255));
			$x = floor($Width/$mCheckCodeNum)*$i;
			$y = rand(0,$Height-15);
			imagechar ($mCheckImage, 5, $x, $y, $mCheckCode[$i], $bg_color);
		}
		imagepng($mCheckImage);
		imagedestroy($mCheckImage);

	}




	/*等比显示图片*/
	/**
	 * 等比显示图片
	 * @access public
	 * @param array $options  数组参数:<br>$file 图片地址<br>$width 宽度<br>$height 高度<br>$R $G $B 背景色<BR>
	 * $dir 保存目录<BR>除file width height 外其它Key均为可选
	 */
	public function  resizeImage(Array $options)
	{
	$file = $options['file'];
	$maxwidth = $options['width'];
	$maxheight = $options['height'];
	isset($options['R']) ? $R=$options['R'] : $R=255;
	isset($options['G']) ? $G=$options['G'] : $G=255;
	isset($options['B']) ? $B=$options['B'] : $B=255;

	//保存目录
	isset($options['dir']) ? $dir=$options['dir'] : $dir=null;
	//保存的完整文件名 $dir=null 是有效
	isset($options['saveFile']) ? $saveFile=$options['saveFile'] : $saveFile=null;
	
	
	/*取得文件后缀*/
	$extend = pathinfo($file);
	$filetype = strtolower($extend["extension"]);
	switch($filetype)
	{
		case 'jpeg':
		case 'jpg':
			$im=imagecreatefromjpeg($file);
			break;
		case 'png':
			$im= imageCreateFromPng($file);
			break;
		case 'gif':
			$im=imageCreateFromGif($file);
			break;
		default:return;
	}


	/*计算宽度*/
	$pic_width = imagesx($im);
	/*高度*/
	$pic_height = imagesy($im);


	$resizewidth_tag = false;
	$resizeheight_tag = false;
	if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
	{
		if($maxwidth && $pic_width>$maxwidth)
		{
			$widthratio = $maxwidth/$pic_width;
			$resizewidth_tag = true;
		}

		if($maxheight && $pic_height>$maxheight)
		{
			$heightratio = $maxheight/$pic_height;
			$resizeheight_tag = true;
		}

		if($resizewidth_tag && $resizeheight_tag)
		{
			if($widthratio<$heightratio)
				$ratio = $widthratio;
			else
				$ratio = $heightratio;
		}

		if($resizewidth_tag && !$resizeheight_tag)
			$ratio = $widthratio;
		if($resizeheight_tag && !$resizewidth_tag)
			$ratio = $heightratio;

		$newwidth = $pic_width * $ratio;
		$newheight = $pic_height * $ratio;

		//if(function_exists("imagecopyresampled")){
			$newim = imagecreatetruecolor($newwidth,$newheight);
			imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
		//}
		//else{
		//	$newim = imagecreate($newwidth,$newheight);
		//	imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
		//}
		/*创建一张指定大小的画布*/
		$img = imagecreatetruecolor($maxwidth,$maxheight);
		/*获取画布背景色*/
		$color = ImageColorAllocate($img,$R,$G,$B);
		/*填充颜色*/
		ImageFill($img,0,0,$color);
		/*缩略图填充至画布，并垂直居中*/
		$x = ($maxwidth-$newwidth)/2;
		$y = ($maxheight-$newheight)/2;
		imagecopyresampled($img,$im,$x,$y,0,0,$newwidth,$newheight,$pic_width,$pic_height);
		/*输出图片*/
		if(empty($dir))
		{
			if (empty($saveFile)){
				@header("Content-Type:image/png");
				imagejpeg($newim);
			}else{
				$exts = pathinfo($saveFile);
				Files::mkdirs($exts['dirname']);
				imagejpeg($newim,$saveFile);
			}
		}
		else
		{
			imagejpeg($newim,$dir.'/'.$maxwidth.'x'.$maxheight.'_'.$extend['filename'].'.'.$extend['extension']);
		}

		imagedestroy($img);
		imagedestroy($im);
		return;
		}
		else
		{
			if(empty($dir))
			{
				if (empty($saveFile)){
					@header("Content-Type:image/png");
					imagejpeg($im);
				}else{
					$exts = pathinfo($saveFile);
					Files::mkdirs($exts['dirname']);
					imagejpeg($im,$saveFile);
				}
			}
			else
			{
				imagejpeg($im,$dir.'/'.$maxwidth.'x'.$maxheight.'_'.$extend['filename'].'.'.$extend['extension']);
			}
			imagedestroy($im);
			return;
		}

	}


}
?>